package com.design.android.personnel.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import androidx.annotation.NonNull;
import com.design.android.personnel.bean.PersonnelInfo;
import com.design.android.personnel.utils.DateUtils;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @author admin
 */
public class PersonnelInfoOperate extends BaseDatabaseOperate<PersonnelInfo> {
    private static final String TABLE_NAME = "personnel_info";

    private static final String[] TABLE_FILED = new String[]{
            "id",
            "userId",
            "avatar",
            "phone",
            "name",
            "sex",
            "addr",
            "createTime",
            "updateTime"
    };

    public PersonnelInfoOperate(@NonNull Context context) {
        super(context);
    }

    @Override
    public void add(@NonNull PersonnelInfo personnelInfo) {
        String createTime = DateUtils.date2Str(personnelInfo.getCreateTime());

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        personnelInfo.getAvatar().compress(Bitmap.CompressFormat.PNG, 100, os);
        byte[] avatarBytes = os.toByteArray();

        ContentValues contentValues = new ContentValues();
        contentValues.put("userId", personnelInfo.getUserId());
        contentValues.put("avatar", avatarBytes);
        contentValues.put("phone", personnelInfo.getPhone());
        contentValues.put("name", personnelInfo.getName());
        contentValues.put("sex", personnelInfo.getSex());
        contentValues.put("addr", personnelInfo.getAddr());
        contentValues.put("createTime", createTime);
        contentValues.put("updateTime", createTime);

        if (database == null || !database.isOpen()) {
            open();
        }
        database.insert(TABLE_NAME, null, contentValues);
        database.close();
    }

    @Override
    public void delete(@NonNull Integer id) {
        if (database == null || !database.isOpen()) {
            open();
        }
        database.delete(TABLE_NAME, "id=?", new String[]{String.valueOf(id)});
        database.close();
    }

    @Override
    public void update(@NonNull PersonnelInfo personnelInfo) {
        Integer id = personnelInfo.getId();

        ByteArrayOutputStream os = new ByteArrayOutputStream();
        personnelInfo.getAvatar().compress(Bitmap.CompressFormat.PNG, 100, os);
        byte[] avatarBytes = os.toByteArray();

        ContentValues contentValues = new ContentValues();
        contentValues.put("userId", personnelInfo.getUserId());
        contentValues.put("avatar", avatarBytes);
        contentValues.put("phone", personnelInfo.getPhone());
        contentValues.put("name", personnelInfo.getName());
        contentValues.put("sex", personnelInfo.getSex());
        contentValues.put("addr", personnelInfo.getAddr());
        contentValues.put("updateTime", DateUtils.date2Str(personnelInfo.getUpdateTime()));

        if (database == null || !database.isOpen()) {
            open();
        }
        database.update(TABLE_NAME, contentValues,
                "id=?", new String[]{String.valueOf(id)});
        close();
    }

    @Override
    public PersonnelInfo select(@NonNull Integer id) {
        if (database == null || !database.isOpen()) {
            open();
        }
        Cursor query = database.query(TABLE_NAME, TABLE_FILED,
                "id=?", new String[]{String.valueOf(id)},
                null, null, null);

        query.moveToFirst();

        PersonnelInfo personnelInfo = new PersonnelInfo();

        personnelInfo.setId(query.getInt(0));
        personnelInfo.setUserId(query.getInt(1));

        byte[] bytes = query.getBlob(2);
        Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, null);

        personnelInfo.setAvatar(bitmap);
        personnelInfo.setPhone(query.getString(3));
        personnelInfo.setName(query.getString(4));
        personnelInfo.setSex(query.getString(5));
        personnelInfo.setAddr(query.getString(6));
        personnelInfo.setCreateTime(DateUtils.str2Date(query.getString(7)));
        personnelInfo.setUpdateTime(DateUtils.str2Date(query.getString(8)));

        close();
        return personnelInfo;
    }

    public List<PersonnelInfo> selectByUserId(@NonNull Integer userId) {
        return select("userId=?", new String[]{String.valueOf(userId)});
    }

    public List<PersonnelInfo> select(String selection, String[] selectionArgs) {
        List<PersonnelInfo> personnelInfoList = new ArrayList<>();

        if (database == null || !database.isOpen()) {
            open();
        }
        Cursor query = database.query(TABLE_NAME, TABLE_FILED,
                selection, selectionArgs,
                null, null, "id desc");

        if (query.getCount() != 0) {
            query.moveToFirst();
            do {
                PersonnelInfo personnelInfo = new PersonnelInfo();

                personnelInfo.setId(query.getInt(0));
                personnelInfo.setUserId(query.getInt(1));

                byte[] bytes = query.getBlob(2);
                Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, null);

                personnelInfo.setAvatar(bitmap);
                personnelInfo.setPhone(query.getString(3));
                personnelInfo.setName(query.getString(4));
                personnelInfo.setSex(query.getString(5));
                personnelInfo.setAddr(query.getString(6));
                personnelInfo.setCreateTime(DateUtils.str2Date(query.getString(7)));
                personnelInfo.setUpdateTime(DateUtils.str2Date(query.getString(8)));

                personnelInfoList.add(personnelInfo);
            } while (query.moveToNext());

        }

        close();
        return personnelInfoList;
    }
}
